package com.draven.serviceImpl;

import com.draven.entity.Module;
import com.draven.mapper.ModuleMapper;
import com.draven.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Draven on 2017/4/13.
 * E-mail:draven1122@163.com
 */
@Service(ModuleService.SERVICE_NAME)
@Transactional(rollbackFor = Exception.class)
public class ModuleServiceImpl implements ModuleService {
    @Autowired
    private ModuleMapper moduleMapper;

    public Module getModule(String id) throws Exception {
        Module module = null;
        module = moduleMapper.selectByPrimaryKey(id);
        return module;
    }

    public int updateModule(Module module) throws Exception {
        int i = 0;
        i = moduleMapper.updateByPrimaryKey(module);
        if (i == 0) {
            throw new SQLException("更新失败");
        }
        return i;
    }

    public int updateModuleSelective(Module module) throws Exception {
        int i = 0;
        i = moduleMapper.updateByPrimaryKeySelective(module);
        if (i == 0) {
            throw new SQLException("更新失败");
        }
        return i;
    }

    public int removeModule(String id) throws Exception {
        int i = 0;
        i = moduleMapper.deleteByPrimaryKey(id);
        if (i == 0) {
            throw new SQLException("删除失败");
        }
        return i;
    }

    public int addModule(Module module) throws Exception {
        int i = 0;
        i = moduleMapper.insert(module);
        if (i == 0) {
            throw new SQLException("新增失败");
        }
        return i;
    }

    public int addModuleSelective(Module module) throws Exception {
        int i = 0;
        i = moduleMapper.insertSelective(module);
        if (i == 0) {
            throw new SQLException("新增失败");
        }
        return i;
    }

    public List<Module> listModulesByParentId(String parent) throws Exception {
        List<Module> modules = new ArrayList<Module>();
        modules = moduleMapper.listModulesByParentId(parent);
        return modules;
    }
}
